package gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import client.ComplaintDetails;
import client.LogIn;
import client.Main;
import client.MainScreen;

public class MyComplaints_GUI extends JFrame implements ActionListener, TableModelListener {
	
	public String[] choice={"non-handled","handled"};
	public String sn[]=new String[3];
	String complaint = null;
	String userID;
	public MainScreen mainScreen = null;
	public Object[][] data = null;
	public String[] columnNames = {"Complaint ID","Complaint Description","Response"};
	
	//buttons
	
	public JButton btnLogOut;
	public JButton btnReviewComp;
	public JButton btnSubmit1;
	public JButton btnBack2;
	public JButton btnBack3;
	
	//panels
	JPanel Mainpanel;
	JPanel panelFileComp;
	JPanel panelShowComp;
	JPanel panelTable;
	
	//text input
	JTextArea textInput;
	JTextArea textID;
	
	//labels
	JLabel lblHeader;
	JLabel lblHeaderFileComp;
	JLabel lblHeaderShowComp;
	JLabel lblHeaderTable;
	JLabel lblText;
	JLabel lblOption;
	JLabel lblID;

	//combo box
	JComboBox options;
	
	Container contentPane;
	
	
	public MyComplaints_GUI(){
		
		super();
		contentPane = this.getContentPane();
	    contentPane.setLayout(new BorderLayout(0, 0));
		this.setBounds((int)Main.dim.getWidth()/2-260, (int)Main.dim.getHeight()/2-275, 515, 550);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setTitle("Zer Lah - File Complaint");
		contentPane.add(getPanelShowComp(),BorderLayout.CENTER);
		this.setSize(500,400);
		this.setVisible(true);
	}
	
	

	JPanel getPanelShowComp()
	{
		if (panelShowComp == null){
			panelShowComp = new JPanel();
			panelShowComp.setSize(500, 400);
			panelShowComp.setLayout(null);
			panelShowComp.add(getBtnBack2());
			panelShowComp.add(getBtnLogOut());
			panelShowComp.add(getCombo());
			panelShowComp.add(getLblOption());
			panelShowComp.add(getLabelShowComp());
			panelShowComp.add(getBtnSubmit1());
		}
		return panelShowComp;
	}
	
	JPanel getPanelTable()
	{
	if (panelTable == null)
		{
		panelTable = new JPanel();
		panelTable.setSize(1000, 400);
		panelTable.setLayout(null);
		panelTable.add(getTable());
		panelTable.add(getBtnBack3());
		panelTable.add(getLabelHeaderTable());
		panelTable.add(getBtnLogOut());
		}
		return panelTable;
	}
	
	JButton getBtnSubmit1() {
			
			if (btnSubmit1 == null){
				btnSubmit1 = new JButton();
				btnSubmit1.setBounds(150,300,200,40);
				btnSubmit1.addActionListener(this);
				btnSubmit1.setText("Submit");
			}
			return btnSubmit1;
		}
	
	JComboBox getCombo()
	{
		if(options==null)
		{
			options=new JComboBox();
			for(int i=0;i<2;i++)
				options.addItem(choice[i]);
				options.setBounds(280, 200, 150, 40);
		}
		return options;
	}
	
	JLabel getLblOption()
	{
		if(lblOption==null)
		{
			lblOption=new JLabel();
			lblOption.setText("Choose an option:");
			lblOption.setBounds(40, 200, 180, 40);
			lblOption.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return lblOption;
	}
	
	JLabel getLabelHeaderTable()
	{
		if(lblHeaderTable==null)
		{
			lblHeaderTable=new JLabel();
			lblHeaderTable.setText("Complaints List");
			lblHeaderTable.setBounds(360, 100, 180, 40);
			lblHeaderTable.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		}
		return lblHeaderTable;
	}
	
	JLabel getLabelShowComp()
	{
		if(lblHeaderShowComp==null)
		{
			lblHeaderShowComp=new JLabel();
			lblHeaderShowComp.setText("My Complaints");
			lblHeaderShowComp.setBounds(150, 100, 180, 40);
			lblHeaderShowComp.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		}
		return lblHeaderShowComp;
	}
	
	JButton getBtnBack2(){
		
		if (btnBack2 == null){
			btnBack2 = new JButton();
			btnBack2.setBounds(380, 20, 100, 40);
			btnBack2.setText("Back");
			btnBack2.addActionListener(this);
		}
		return btnBack2;
	}
	
	JButton getBtnBack3(){
		
		if (btnBack3 == null){
			btnBack3 = new JButton();
			btnBack3.setBounds(880, 20, 100, 40);
			btnBack3.setText("Back");
			btnBack3.addActionListener(this);
		}
		return btnBack3;
	}
	
	JButton getBtnLogOut() {
		
		if (btnLogOut == null){
			btnLogOut = new JButton();
			btnLogOut.setBounds(20,20,100,40);
			btnLogOut.addActionListener(this);
			btnLogOut.setText("LogOut");
		}
		return btnLogOut;
	}
	
	JScrollPane getTable(){
		
		  DefaultTableModel model = new DefaultTableModel(data, columnNames) {
		      public boolean isCellEditable(int rowIndex, int mColIndex) {
		      return false;
		      }
		  };
		  JTable table = new JTable(model); 
		  table.getModel().addTableModelListener(this);
		  TableColumn column = table.getColumnModel().getColumn(2);
		  column.setPreferredWidth(200);
		  column = table.getColumnModel().getColumn(0);
		  column.setPreferredWidth(1);
		  column = table.getColumnModel().getColumn(1);
		  column.setPreferredWidth(5);
	      JScrollPane scroll = new JScrollPane(table); 
	      scroll.setBounds(90, 150, 800, 190);
	      table.setBounds(90, 150, 800, 190);
	      return scroll; 
	}

	
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource() == btnSubmit1)
		{
			panelShowComp=null;
			sn[0] = "get my complaints";
			sn[1] = (String) options.getSelectedItem();
			sn[2] = Integer.toString(LogIn.usr.getID());
			data = ComplaintDetails.getComplaintDetails(sn);
			this.getContentPane().removeAll();
			this.setSize(1000, 400);
			this.setBounds(150, 260, 1000, 400);
			this.getContentPane().add(getPanelTable(),BorderLayout.CENTER);
			validate();
			setVisible(true);
		}
		
		if(e.getSource() == btnLogOut)
		{
			if(JOptionPane.showConfirmDialog(null,"Are you sure you want to logout?",null,JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
			{
				this.setEnabled(false);
				this.setVisible(false);
				Main.main.logOut.doClick();
			}
		}
		
		if(e.getSource() == btnBack3)
		{
			panelTable=null;
			this.getContentPane().removeAll();
			this.getContentPane().add(getPanelShowComp(),BorderLayout.CENTER);
			this.setBounds((int)Main.dim.getWidth()/2-260, (int)Main.dim.getHeight()/2-275, 515, 550);
			this.setSize(500, 400);
			validate();
			setVisible(true);
		}
		
	}



	public void tableChanged(TableModelEvent arg0) {		
	}

}
